group = {};

group.admin = liwe.module ( 'group.admin' );

group.admin._modules = null;
group.admin.admin_man = null;

group.admin._curr_gid = null;
group.admin._id_name = [];
group.admin._u2g = {}; 
//DIVS
group.admin._right_top_div = 'user-admin-perms';
group.admin._left_bot_am_div = 'user-admin-groups';
group.admin._panels_div = 'user-admin-panels'; 
group.admin._filter_panels_div = 'user-admin-pan-filter'; 

group.admin._admin_manager_init = function ( dest_div )
{
	if ( group.admin.admin_man ) return;

	if ( ! dest_div ) dest_div = user.admin._left_bot_div; 

	group.admin.admin_man = new AdminManager ( { 
		'name' : 'groups-list',
		'dest_res': dest_div,
		'list_action': "user.group_admin.list",
		'del_action': 'user.group_admin.delete',
		'glob_menu_actions': [], 
		'templates' : {
			"del_obj" : '<div class="group_del_icon" title="Elimina" onclick="AdminManager.row_action(\'%(_id)s\',\'del_item\', \'%(_am_name)s\' )" ></div>',
			"edit_obj" : '<div class="group_edit_icon" title="Modifica" onclick="AdminManager.row_action(\'%(_id)s\',\'edit_item\',\'%(_am_name)s\')"></div>'
			},
		'mod_menu_actions': [
			 { func: 'group.admin.add ()', 'class' : 'user-add-group' , label: 'Aggiungi gruppo' }
						]
	} );

	group.admin.admin_man.set_row_fields ( [ 
						[ "GID", "gid" ], 
						[ "Gruppo", "name" ], 
						[ "Descrizione", "descr" ] 
					     ] );

	group.admin.admin_man.set_row_actions ( [ 
						[ '<img border="0" src="/modules/user/gfx/setting_tools.png" alt="Setta i permessi"  title="Setta i permessi"/>', "set_perms" ]
					] );

	group.admin.admin_man.cbacks [ 'row_manip' ] = group.admin.row_manip;

	group.admin.admin_man.cbacks [ 'edit_item' ] = function ( id ) { group.admin.add ( group.admin._right_top_div, id ); };
	group.admin.admin_man.cbacks [ 'del_item' ]  = function ( id ) { group.admin.del ( id ); };

	group.admin.admin_man.cbacks [ 'set_perms' ] = function ( id ) { user.admin._set_perms ( user.admin._right_top_div, id, 'group' ); };
};

group.admin.row_manip = function ( ds, row )
{
	row [ '_id' ] = row [ 'gid' ];
	row [ 'id' ] = row [ 'gid' ];
	group.admin._id_name [ row [ 'gid' ] ] = row [ 'name' ];
};

group.admin.get_user_groups = function ( uid, cback )
{
	var vals = { action: 'user.group_admin.list', uid: uid, full : 'yes' }; 
	group.admin.ajax ( vals,  function ( data ) 
					{
						if ( ! data.get ( 'groups' ) ) return;
						var gids_lst = [];
						group.admin._u2g [ uid ] = {};
						data.groups.iterate ( function ( v )
									{
										group.admin._u2g [ uid ] [ v.name ] = v;
										gids_lst.push ( v.gid );				
										console.debug ( "group.admin.get_user_groups: v = %o" , v );
									} );
						if ( cback ) cback ( data.groups );
					} );
};

group.admin.add = function ( dest_div, id )
{
	if ( ! dest_div ) dest_div = group.admin._right_top_div;
	group.admin._curr_gid =  ( ! id ) ? null : id;

	if ( ! id )
	{
		group.admin.create ( dest_div );
		return;
	}
	
	group.admin.ajax ( { action: "user.group_admin.get", gid: id }, function ( v )
	{
		group.admin.create ( dest_div, v [ 'group' ] );
		user.admin._filter_form ( group.admin._filter_panels_div, 'user-panels-filter', function () 
			{
				group.admin.manage_group_user ( group.admin._curr_gid );
			} );
	});
};

group.admin.create = function ( dest_div, dct )
{
	if ( ! dest_div ) dest_div = group.admin._right_top_div;
	if ( ! dct ) 
	{
		if ( $( group.admin._panels_div ) ) $( group.admin._panels_div , '' );
		dct = {};
	}

	var gid = dct.get ( 'gid', '' )
	var data = { "title" : ( gid ? "Modifica gruppo " + group.admin._id_name [ gid ] : "Creazione nuovo gruppo" ) };

	$( dest_div, String.formatDict ( user.templates [ 'user_create' ], data ) );

	var f = new liwe.form.instance ( "group-frm" );

	f.hidden ( 'gid', gid );
	f.hidden ( 'action', "user.group_admin.create" );

	f.text ( { name: 'name', label: 'Nome', mandatory: true, value: dct.get ( 'name', '' ) } );
	f.text ( { name: 'descr', label: 'Descrizione', value: dct.get ( 'descr', '' ) } );

	f.button ( { name: 'btn_save', value: 'Salva', onclick: "group.admin.create_done()" } );
	
	f.workspace ( { name: 'user-manage-group' } );

	f.set ( 'user_cnt_form' );
};

group.admin.create_done = function ()
{
	var f = liwe.form.get ( "group-frm" );

	if ( ! f.check () ) return;

	var dct = f.get_values ();

	group.admin.ajax ( dct, function ( v )
	{
		group.admin.list_groups ();
	} );
};

group.admin.manage_group_user = function ( id )
{
	$( group.admin._panels_div, 'Searching ...' );
	var filter = {}, f = liwe.form.get ( 'user-panels-filter' );
	if ( f )
		filter = f.get_values ();
	//user-admin-groups
	var pan_groups = {};
	var attrs = {
			head_left: 'Utenti non nel gruppo',
			head_right: 'Utenti nel gruppo'
			};
	var sel_id = 'user_in_group';
	var gr = 'right';
	var pan = new WWL.panels ( group.admin._panels_div, attrs );
	
	function _render ()
	{
		pan.set_cbacks ( 'done', group.admin.save_group_user );
		pan.render ( group.admin._panels_div );
		pan.populate ( pan_groups [ 'left' ] , pan_groups [ 'right' ] );
	};

	function _req_data ( cback )
	{
		group.admin.ajax ( vals, function ( data )
			{
				if ( $( sel_id ) ) $( sel_id, '' );
				if ( ! data.get ( 'users' ) ) return;
				pan_groups [ gr ] = [];
				data.users.iterate ( function ( v, k )
						{
							pan_groups [ gr ].push ( { id: v.uid, name: v.login, title: v.email } );
						} );
				if ( cback ) 
				{
					vals [ 'not_in_group' ] = 'yes';
					sel_id = 'user_not_in_group';
					gr = 'left'
					cback ( _render );
				};
			}); 
	};

	var vals = {
			action : 'user.group_admin.get_group_user',
			gid : id
			};
	vals.inherits ( filter );

	_req_data ( _req_data );
};

group.admin.save_group_user = function ( groups_lst )
{
	var gid = group.admin._curr_gid;
	if ( ! gid ) return;
	var vals = {
			action : 'user.group_admin.save_group_user',
			gid : gid
			};
			
	ids_lst = groups_lst [ 'group_right' ];
	if ( ids_lst.length < 1 ) vals [ 'action' ] = 'user.group_admin.empty_group_user';
	vals [ 'ids' ] = ids_lst;
	group.admin.ajax ( vals, function ( data ) 
				{
					group.admin._u2g = {}; 
					liwe.utils.notifier.show ( 'Modifiche utenti gruppo ' + gid + ' salvate', '', 1500 );
				} );

};

group.admin.list_groups = function ()
{
	group.admin.admin_man.show_list ();
};

group.admin.del = function ( ids )
{
	if ( ! ids ) return;
	//var comma = ids.replace ( /\|/g, ' , ' );

	var comma = ids.split ( '|' );
	comma.iterate ( function ( v, k ) { comma [ k ] =  group.admin._id_name [ v ] } );
	comma.join ( ' , ' );

	var uconfirm = confirm ( 'Sicuro di voler eliminare il gruppo ' + comma + ' ?' );
	if ( ! uconfirm ) return;
	var vals = {
			'action': 'user.group_admin.delete',
			'ids' : ids
			};
	group.admin.ajax ( vals , function () 
		{
			liwe.utils.notifier.show ( 'Eliminazione  gruppo/i effettuata ( ' + comma + ' )', '', 1500 );
			group.admin.list_groups ();
		} );
};

/*
	Check if admin.user has been defined and templates loaded
*/
group.admin._check_user_obj = function ()
{
	if ( ! user.admin )
	{
		alert ( 'La gestione degli utenti non è stata inizializzata' );
		return;
	}
	if ( ! user.admin.get ( 'templates' ) )
	{
		//user.admin._init_templates ( group.admin.init );
		setTimeout ( group.admin.init , 300 );
	}
};

group.admin._clear_group_divs = function ()
{
	if ( $( group.admin._panels_div ) ) $( group.admin._panels_div, '' );
	if ( $( group.admin._filter_panels_div ) ) $( group.admin._filter_panels_div, '' );
};

group.admin.init = function ()
{
	//group.admin._check_user_obj ();
	group.admin._admin_manager_init ( group.admin._left_bot_am_div );	
};
